package gameengine.com.actor;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint.Style;

import gameengine.com.MainActivity;
import gameengine.com.Map;
import gameengine.com.Sprite;
import gameengine.com.state.StateGameplay;

public class Creature extends Actor {
	public static final int STATE_IDLE = 0;
	public static final int STATE_MOVE = 1;
	public static final int STATE_HURT = 2;
	public static final int STATE_DIE = 3;

	
	public static final int TYPE_WHILE = 0;
	public static final int TYPE_RED = 1;
	public static final int TYPE_SCORPIO =2;
	
	public static final int ANIM_IDLE_DOWN = 0;
	public static final int ANIM_IDLE_LEFT = 1;
	public static final int ANIM_IDLE_RIGHT = 2;
	public static final int ANIM_IDLE_UP = 3;
	public static final int ANIM_RUN_DOWN = 4;
	public static final int ANIM_RUN_LEFT = 5;
	public static final int ANIM_RUN_RIGHT = 6;
	public static final int ANIM_RUN_UP = 7;
	
	public int type = 0;
	public static int MAX_STEP = 2;
	public int mMoveStep = 0;
	public boolean ismove = false;

	public Creature(Sprite paramImage, int row, int col, int w, int h,int _type) {
		// super(paramImage, 18, 18);
		type = _type;
		m_pos_rows = row;
		m_pos_cols = col;
		sprite = paramImage;
		m_x = col * Map.MAP_CELL_WIDTH + Map.MAP_CELL_WIDTH / 2;
		m_y = row * Map.MAP_CELL_HEIGHT + Map.MAP_CELL_HEIGHT / 2;
		m_Width = w;
		m_Height = h;

		this.mdirection = 0;
		if(_type == TYPE_WHILE){
			sprite = new Sprite("sprite/actor/chicken.sprt");
		}
		else if(_type == TYPE_RED){
			sprite = new Sprite("sprite/actor/mushroom1.sprt");
		}
		
		sprite.setAnim(0,m_x,m_y, true, false);
		// this.isFinished = false;
	}

	public void setstate(int state) {

		mstate = state;
		switch (mstate) {

		case STATE_IDLE:
			ismove = false;
			break;
		case STATE_MOVE:
			ismove = false;
			mMoveStep = MAX_STEP;
			if(type == TYPE_SCORPIO)
			{
				mMoveStep = 1;// bo cap chi di chuyen 1 buoc
			}
			break;
		case STATE_HURT:
			ismove = false;
			break;
		case STATE_DIE:
			ismove = false;
			break;
		default:
			break;
		}
	}

	public void draw(Canvas c) {
		sprite.drawAnim(c);
		/*	switch (mstate) {
		case STATE_IDLE:
			if(type == TYPE_WHILE)
				MainActivity.mainPaint.setColor(Color.WHITE);
			else if(type == TYPE_RED)
				MainActivity.mainPaint.setColor(Color.RED);
			break;
		case STATE_MOVE:
			MainActivity.mainPaint.setColor(Color.BLUE);
			break;
		case STATE_HURT:
			MainActivity.mainPaint.setColor(Color.GREEN);
			break;
		case STATE_DIE:
			MainActivity.mainPaint.setColor(Color.YELLOW);
			break;
		}
		sprite.drawAnim(c);
		
		switch (type) {
		case TYPE_WHILE:
			MainActivity.mainPaint.setStyle(Style.FILL);
			
			c.drawCircle(m_x, m_y, m_Width, MainActivity.mainPaint);
			
			sprite.drawAnim(c);
			break;
		case TYPE_RED:			
			MainActivity.mainPaint.setStyle(Style.FILL);
			c.drawCircle(m_x, m_y, m_Width, MainActivity.mainPaint);
			break;
		case TYPE_SCORPIO:
			MainActivity.mainPaint.setStyle(Style.STROKE);
			c.drawRect(m_x - m_Width/2, m_y -m_Height/2, m_x + m_Width/2, m_y + m_Height/2, MainActivity.mainPaint);
			break;
		default:
			
			break;
		}*/
	
	}

	public void update() {
		switch (mstate) {
		case STATE_IDLE:
			updateStateIDE();
			break;
		case STATE_MOVE:
			updateStateMove(StateGameplay.explorer);
			break;
		case STATE_HURT:

			break;
		case STATE_DIE:

			break;
		default:
			break;
		}

	}

	void updateStateIDE() {
		if(m_pos_rows == StateGameplay.explorer.m_pos_rows && m_pos_cols == StateGameplay.explorer.m_pos_cols)
		{
			Explorer.isDie = true;
		}
	}

	void updateStateMove(Explorer explorer) {
		if (ismove) {
			updateMoving();
		} else if (mMoveStep > 0) {

			getNextPost(explorer);
		} else {
			setstate(STATE_IDLE);
		}

	}

	public void getNextPost(Explorer explorer) {
		switch (type) {
		case TYPE_WHILE:
		case TYPE_SCORPIO:
			if (m_pos_cols != explorer.m_pos_cols) // set theo chieu x truoc
			{
				if (m_pos_cols > explorer.m_pos_cols) {
					mdirection = DIRECTION_LEFT;
					sprite.setAnim(ANIM_IDLE_LEFT,m_x,m_y, true, false);
					m_Target_Pos_Rows = m_pos_rows;
					m_Target_Pos_Cols = m_pos_cols - 1;
				} else if (m_pos_cols < explorer.m_pos_cols) {
					mdirection = DIRECTION_RIGHT;
					sprite.setAnim(ANIM_IDLE_RIGHT,m_x,m_y, true, false);
					m_Target_Pos_Rows = m_pos_rows;
					m_Target_Pos_Cols = m_pos_cols + 1;
				}

			} else if (m_pos_rows != explorer.m_pos_rows) // set theo chieu y
															// sau
			{

				if (m_pos_rows > explorer.m_pos_rows) {
					mdirection = DIRECTION_UP;
					sprite.setAnim(ANIM_IDLE_UP,m_x,m_y, true, false);
					m_Target_Pos_Rows = m_pos_rows - 1;
					m_Target_Pos_Cols = m_pos_cols;
				} else if (m_pos_rows < explorer.m_pos_rows) {
					mdirection = DIRECTION_DOWN;
					sprite.setAnim(ANIM_IDLE_DOWN,m_x,m_y, true, false);
					m_Target_Pos_Rows = m_pos_rows + 1;
					m_Target_Pos_Cols = m_pos_cols;
				}

			}
			
			
			break;
		case TYPE_RED:
			if (m_pos_rows != explorer.m_pos_rows) // set theo chieu truoc
			{

				if (m_pos_rows > explorer.m_pos_rows) {
					mdirection = DIRECTION_UP;
					m_Target_Pos_Rows = m_pos_rows - 1;
					m_Target_Pos_Cols = m_pos_cols;
				} else if (m_pos_rows < explorer.m_pos_rows) {
					mdirection = DIRECTION_DOWN;
					m_Target_Pos_Rows = m_pos_rows + 1;
					m_Target_Pos_Cols = m_pos_cols;
				}

			} else if (m_pos_cols != explorer.m_pos_cols) // set theo chieu x
															// sau
				{
				if (m_pos_cols > explorer.m_pos_cols) {
					mdirection = DIRECTION_LEFT;
					m_Target_Pos_Rows = m_pos_rows;
					m_Target_Pos_Cols = m_pos_cols - 1;
				} else if (m_pos_cols < explorer.m_pos_cols) {
					mdirection = DIRECTION_RIGHT;
					m_Target_Pos_Rows = m_pos_rows;
					m_Target_Pos_Cols = m_pos_cols + 1;
				}

			} 
			break;
		}
		
		if(StateGameplay.map.checkWallCollition(mdirection, m_pos_cols, m_pos_rows))
		{
			if(mdirection == DIRECTION_LEFT || mdirection == DIRECTION_RIGHT)
			{
				m_Target_Pos_Cols = m_pos_cols;
				if(m_pos_rows < explorer.m_pos_rows && !StateGameplay.map.checkWallCollition(DIRECTION_DOWN, m_pos_cols, m_pos_rows))
				{
					m_Target_Pos_Rows = m_pos_rows + 1;
					mdirection = DIRECTION_DOWN;
				}
				else if(m_pos_rows > explorer.m_pos_rows && !StateGameplay.map.checkWallCollition(DIRECTION_UP, m_pos_cols, m_pos_rows))
				{
					m_Target_Pos_Rows = m_pos_rows - 1;
					mdirection = DIRECTION_UP;
				}
			}
			else if(mdirection == DIRECTION_UP || mdirection == DIRECTION_DOWN)
			{
				m_Target_Pos_Rows = m_pos_rows;
				if(m_pos_cols < explorer.m_pos_cols && !StateGameplay.map.checkWallCollition(DIRECTION_RIGHT, m_pos_cols, m_pos_rows))
				{
					m_Target_Pos_Cols = m_pos_cols + 1;
					mdirection = DIRECTION_RIGHT;
				}
				else if(m_pos_cols > explorer.m_pos_cols && !StateGameplay.map.checkWallCollition(DIRECTION_LEFT, m_pos_cols, m_pos_rows))
				{
					m_Target_Pos_Cols = m_pos_cols - 1;
					mdirection = DIRECTION_LEFT;
				}
			}
		}
		
		if (m_Target_Pos_Cols != -1 && m_Target_Pos_Rows != -1) {
			if (m_Target_Pos_Rows != m_pos_rows || m_Target_Pos_Cols != m_pos_cols) {
				m_Target_x = m_Target_Pos_Cols * Map.MAP_CELL_WIDTH + Map.MAP_CELL_WIDTH / 2;
				m_Target_y = m_Target_Pos_Rows * Map.MAP_CELL_HEIGHT + Map.MAP_CELL_HEIGHT / 2;
				ismove = true;
			} else if (m_Target_Pos_Rows == m_pos_rows && m_Target_Pos_Cols == m_pos_cols) {
				setstate(STATE_IDLE);
			}
		}

	}

	void updateMoving() {

		if ((Math.abs(m_x - m_Target_x) <= m_Speed) && ((m_x - m_Target_x) != 0)) {
			m_x = m_Target_x;
			m_pos_cols = m_Target_Pos_Cols;
			m_pos_rows = m_Target_Pos_Rows;
			ismove = false;
			mMoveStep--;
			return;// vi chi duy chuyen theo doc hoac ngang
		} else if ((Math.abs(m_y - m_Target_y) <= m_Speed) && ((m_y - m_Target_y) != 0)) {
			m_y = m_Target_y;
			m_pos_cols = m_Target_Pos_Cols;
			m_pos_rows = m_Target_Pos_Rows;

			ismove = false;
			mMoveStep--;
			return;// vi chi duy chuyen theo doc hoac ngang
		}
		switch (mdirection) {
		case DIRECTION_UP:
			m_y -= m_Speed;
			break;
		case DIRECTION_DOWN:
			m_y += m_Speed;
			break;
		case DIRECTION_LEFT:
			m_x -= m_Speed;
			break;
		case DIRECTION_RIGHT:
			m_x += m_Speed;
			break;
		default:
			break;
		}
		sprite.setPos(m_x, m_y);
	}
	
}
